package cn.edu.jlu.grid.vjm;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class VJob {
	private static int id = 0;
	private final int ID = id++;
	private String gkContact;
	private InetAddress vjobMsgCenterAddress;
	private int vjobMsgCenterPort;
	
	public int getId() {
		return ID;
	}

	public String getGkContact() {
		return gkContact;
	}

	public void setGkContact(String gkContact) {
		this.gkContact = gkContact;
	}

	public InetAddress getVjobMsgCenterAddress() {
		return vjobMsgCenterAddress;
	}

	public void setVjobMsgCenterAddress(InetAddress vjobMsgCenterAddress) {
		this.vjobMsgCenterAddress = vjobMsgCenterAddress;
	}

	public int getVjobMsgCenterPort() {
		return vjobMsgCenterPort;
	}

	public void setVjobMsgCenterPort(int vjobMsgCenterPort) {
		this.vjobMsgCenterPort = vjobMsgCenterPort;
	}
	
	public void release() {
		Socket socket = null;
		try {
			socket = new Socket(vjobMsgCenterAddress, vjobMsgCenterPort);
			ObjectOutputStream os = new ObjectOutputStream(socket.getOutputStream());
			os.writeObject(new VJobReleaseMessage());
			os.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try { socket.close(); } catch(Exception e) {}
		}
		System.out.println("Send VJobReleaseMessage to vjob: " + getId() +
				" at :" + vjobMsgCenterAddress.getHostAddress() +
				":" + vjobMsgCenterPort);
	}
	
	public String toString() {
		return "VJob id: " + getId() + 
			"\nHost: " + vjobMsgCenterAddress + 
			"\nResourceContact: " + gkContact;
	}
}
